const http = require('http');
const getDataList = require('./mockData.js');

const server = http.createServer((req, res) => {
    const url = req.url;
    console.log(url);
    // 发送 data 由 message组成 \n\n
    // mesage id,data,retry,event
    // data:数据
    if (url == '/getlist') {
        res.writeHead(200, {
            'Content-Type': 'text/event-stream',
            'Cache-Control': 'no-cache',
            Connection: 'keep-alive',
            'Access-Control-Allow-Origin': '*', // 解决跨域
        });
        res.write('retry: 10000\n');
        let interval = setInterval(function () {
            res.write('data: ' + JSON.stringify(getDataList()) + '\n\n');
        }, 1000 * 5);
        req.connection.addListener(
            'close',
            function () {
                clearInterval(interval);
            },
            false
        );
    }
    // res.end()
});

server.listen(9800, () => {
    console.log('服务已启动。。。端口9528');
});
